﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using MiniFox.Components;
using MiniFox.Log;
using MiniFox.Platform;
using MiniFox.Platform.File;
using MiniFox.Resources;
using MiniFox.Web.Infrastructure;
using Platform.Core.Services;
using System.ComponentModel.DataAnnotations;

namespace Platform.Web.Controllers
{
    /// <summary>
    /// 
    /// </summary>
    [AllowAnonymous]
    public class RandController : BinaryControllerBase
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="group"></param>
        /// <returns></returns>
        protected virtual IRandomFileLoader CreateComponent(string group)
        {
            var _component = ComponentFactory.GetComponent<IRandomFileLoader>(group);
            return _component;
        }
        /// <summary>
        /// 下载文件
        /// </summary>
        /// <param name="group">资源管理组名</param>
        /// <returns></returns>
        [HttpGet, NoLog]
        public async Task<IActionResult> LoadAsync([Required(ErrorMessageResourceType = typeof(CommonMessages), ErrorMessageResourceName = "MSG_CommonRequired"), FromRoute(Name = GlobalKey.NAME)] string group)
        {
            var component = this.CreateComponent(group);
            var fileData = await component.RandomLoadAsync(string.Empty);
            var result = this.Execute(fileData.Data, fileData.ContentType, fileData.FileName);
            return result;
        }
    }
}
